#region Imported Namespaces


using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;


#endregion


namespace WtfManager.UIManager
{
  internal class DragStartInfo
  {
    #region Properties/Indexers


    public UIElement Container
    {
      get;
      set;
    }

    public DataObject DragDataObject
    {
      get;
      set;
    }

    public List<object> DraggedItems
    {
      get;
      set;
    }

    public UIElement DragInitiator
    {
      get;
      set;
    }

    public Point StartPosition
    {
      get;
      set;
    }


    #endregion


    #region Methods


    public List<UIElement> GetItemContainers()
    {
      return GetItemContainers(Container, Container is TreeView);
    }


    private List<UIElement> GetItemContainers(object container, bool recursive)
    {
      var containers = new List<UIElement>();

      if (container is ItemsControl)
      {
        var itemsControl = (ItemsControl)container;

        ItemContainerGenerator generator = itemsControl.ItemContainerGenerator;

        if (generator.Status != GeneratorStatus.ContainersGenerated)
        {
          return containers;
        }

        foreach (object item in DraggedItems)
        {
          DependencyObject itemContainer = generator.ContainerFromItem(item);

          if (itemContainer is UIElement)
          {
            containers.Add((UIElement)itemContainer);
          }
        }

        if (recursive)
        {
          foreach (object item in itemsControl.Items)
          {
            if (containers.Count == DraggedItems.Count)
            {
              return containers;
            }

            DependencyObject itemContainer = generator.ContainerFromItem(item);
            containers.AddRange(GetItemContainers(itemContainer, true));
          }
        }
      }

      return containers;
    }


    #endregion
  }
}